package com.zff.tool;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.zff.controller.UploadController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * TODO
 * @version 1.0
 * @author Zff
 * @date 2020/8/24 15:00
 */
public class QiniuUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(QiniuUtil.class);
    //测试文件上传
    public static String testUpload(byte[] bytes, MultipartFile file ,String userId,String fileName1,String fileName2){
//构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.huadong());
//...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证，然后准备上传
        String accessKey = "8qImTte1BjjYIfQO1zDCn1IL-NSEgqR3KXm8A6ZA";
        String secretKey = "vSvqgzTlAhbBWe-tC3S5UzQgXHa9CTchF1E4YmmJ";
        String bucket = "yuwenbaoshe";
        //String key = UUID.randomUUID().toString();
        String res = null;
//默认不指定key的情况下，以文件内容的hash值作为文件名
        FileInputStream fileInputStream = null;
        try {
            //String fileName1 = file.getOriginalFilename();
            String fileName = fileName1;
            SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd");
            String format = sdf.format(new Date());
            //String filePath = "E:\\test\\";
            String filePath = "/"+format+"/";
            System.out.println("上传文件路径realPath:"+filePath);
            File folder = new File(filePath);
            if(!folder.exists()){
                folder.mkdirs();
            }
            File dest = new File(filePath + fileName2);
            try {
                file.transferTo(dest);
                LOGGER.info("上传成功");
            } catch (IOException e) {
                LOGGER.error(e.toString(), e);
            }
            fileInputStream = new FileInputStream(dest);
            //byte[] bytes = IOUtils.toByteArray(fileInputStream);
            System.out.println(bytes);
           // byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);
            try {
                Response response = uploadManager.put(bytes, fileName, upToken);
                res = response.bodyString();
                System.out.println("@@@@@@:"+response.bodyString());
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (FileNotFoundException ex) {
            //ignore
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }


}
